﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VideoLib.Models;
using ListBox = System.Windows.Controls.ListBox;

namespace VideoLib
{
    /// <summary>
    /// ThumbWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ThumbWindow : Window
    {
        public ThumbWindow(ObservableCollection<PreviewInfo> bitmapImages, string fileName)
        {
            PreviewImage = new ObservableCollection<PreviewInfo>(bitmapImages);
            FileName = fileName;
            InitializeComponent();
            DataContext = this;
            StateChanged += OnStateChanged;
        }

        private void OnStateChanged(object? sender, EventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                GridRoot.Margin = new Thickness(8);
            }
            else
            {
                GridRoot.Margin = new Thickness(0);
            }
        }

        public ObservableCollection<PreviewInfo> PreviewImage { get; set; }
        public string FileName { get; set; }
        private void ZoomBorder_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ZoomBorder.Reset();
        }

        private void CloseBt_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void ImageList_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollViewer scrollViewer = FindVisualChild<ScrollViewer>(sender as ListBox);

            if (scrollViewer != null)
            {
                if (e.Delta > 0)
                    scrollViewer.PageLeft();
                else
                    scrollViewer.PageRight();

                e.Handled = true;
            }
        }

        // 一个辅助方法，用于查找 ListBox 中的 ScrollViewer
        private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is T)
                    return (T)child;
                else
                {
                    T childOfChild = FindVisualChild<T>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }
}
